package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopCategoryRelationBO;
import com.ztgf.shopping.models.dto.shop.ShopCategoryRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopCategoryRelationService;
import com.ztgf.shopping.models.vo.shop.CommodityCategoryTreeVO;
import com.ztgf.shopping.models.vo.shop.ShopBrandRelationNewVO;
import com.ztgf.shopping.models.vo.shop.ShopCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 店铺分类表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:42:59
 */

@RestController
@RequestMapping("/cms/shopCategoryRelation")
@Slf4j
@Api(value = "ShopCategoryRelationController", tags = "店铺分类表接口")
public class ShopCategoryRelationController {

    @DubboReference
    private IShopCategoryRelationService shopCategoryRelationService;

    @PostMapping("/getCategoryByShopId")
    @ApiOperation(value = "根据店铺ID查找类目佣金", notes = "根据店铺ID查找类目佣金")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<List<ShopCategoryVO>> getCategoryByShopId(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        List<ShopCategoryVO> vos = shopCategoryRelationService.getCategoryByShopId(id);
        return new Result<>(vos);
    }

    @PostMapping("/queryListByBrandId")
    @ApiOperation(value = "根据品牌id查找列表", notes = "根据品牌id查找列表")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<List<ShopCategoryRelationDTO>> queryListByBrandId(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        List<ShopCategoryRelationDTO> dtos = shopCategoryRelationService.queryListByBrandId(id);
        return new Result<>(dtos);
    }

    @PostMapping("/queryTreeByBrandId")
    @ApiOperation(value = "根据品牌id查找类目树", notes = "根据品牌id查找类目树")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<List<CommodityCategoryTreeVO>> queryTreeByBrandId(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        List<CommodityCategoryTreeVO> vos = shopCategoryRelationService.queryTreeByBrandId(id);
        return new Result<>(vos);
    }

    @ApiOperation(value = "店铺分类表列表", notes = "店铺分类表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopCategoryRelationDTO>> queryPageList(ShopCategoryRelationBO bo) {
        Wrapper<ShopCategoryRelationDTO> wrapper = new EntityWrapper<>();

        Page<ShopCategoryRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopCategoryRelationService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/getBrandByBrandId")
    @ApiOperation(value = "后台管理系统品牌详情根据品牌id获取关联类目和店铺", notes = "后台管理系统品牌详情根据品牌id获取关联类目和店铺")
    public Result getBrandNewByBrandId(Long brandId) {
        if (brandId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            ShopBrandRelationNewVO vo = shopCategoryRelationService.getBrandNewByBrandId(brandId);
            return new Result<>(vo);
        }
    }

    @PostMapping("/updateShopDividendScale")
    @ApiOperation(value = "修改店铺类目佣金", notes = "修改店铺类目佣金")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true, paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "ids", value = "主键id", required = true, paramType = "query", dataType = "String")
            , @ApiImplicitParam(name = "dividendScaleRate", value = "佣金比例", required = true, paramType = "query", dataType = "BigDecimal")
    })
    public Result updateShopDividendScale(Long shopId,String ids, BigDecimal dividendScaleRate) {
        if (shopId==null || dividendScaleRate == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag =  shopCategoryRelationService.updateShopDividendScale(shopId,ids,dividendScaleRate);
        if (flag){
            return new Result<>(CodeEnum.SUCCESS);
        }else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
}
